package com.symria.system.util;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Locale;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class TemplateUtil {
	private static TemplateUtil util = null;
	private Configuration cfg = new Configuration();
	
	public TemplateUtil(){
		cfg = new Configuration();
		cfg.setEncoding(Locale.getDefault(), "utf-8");
	}
	
	public static TemplateUtil getInstance(){
		if(util==null){
			util = new TemplateUtil();
		}
		return util;
	}
	
	public String process(String templateName, String str, Map root) throws TemplateException, IOException{
		Template temp = new Template(templateName, new StringReader(str), cfg);
		StringWriter writer = new StringWriter();
		temp.process(root, writer);
		return writer.toString();
	}
}
